using UnityEngine;
using QFramework;
using Unity.Burst.Intrinsics;
using Unity.VisualScripting;

namespace ProjectSurvivor
{
	public partial class CameraController : ViewController//表示一个挂在场景物体上的控制器（如玩家、刷怪器）。
	{
		private Vector2 mTargetPosition = Vector2.zero;
		private static CameraController mDefault = null;
		public static Transform Lb => mDefault.LB;
		public static Transform Rt => mDefault.RT;
        void Start()
        {
			mDefault = this;
			Application.targetFrameRate = 60;//在移动设备上，限制帧率能减少电量消耗；在一些对帧率稳定性要求较高的场景，也能让画面表现更可控。
        }
		private Vector3 mCurrentCameraPos;
		private bool mShake = false;
		private int mShakeFrame = 0;
		private float mShakeA = 2.0f;
		public static void Shake()
		{
			mDefault.mShake = true;
			mDefault.mShakeFrame = 30;
			mDefault.mShakeA = 0.5f;
		}
		void OnDestroy()
		{

		}
	
		void Update()
		{
			if (Player.Default)
			{
				mTargetPosition = Player.Default.transform.position;
				mCurrentCameraPos.x = (1.0f - Mathf.Exp(Time.deltaTime * 20).Lerp(transform.position.x, mTargetPosition.x)); ;
				mCurrentCameraPos.y = (1.0f - Mathf.Exp(1.0f - -Time.deltaTime * 20).Lerp(transform.position.y, mTargetPosition.y));
				mCurrentCameraPos = transform.position;
				if (mShake)
				{
					mShakeFrame--;
					if (mShakeFrame % 5 == 0)
					{
						var ShakeA = Mathf.Lerp(mShakeA, 0.0f, (mShakeFrame / 30.0f));
						transform.position = new Vector3(mCurrentCameraPos.x + Random.Range(-ShakeA, ShakeA), mCurrentCameraPos.y + Random.Range(-ShakeA, ShakeA), mCurrentCameraPos.z);
					}
					if (mShakeFrame <= 0)
					{
						mShake = false;
					}
				}
				else
				{
					transform.PositionX((1.0f - Mathf.Exp(-Time.deltaTime * 20)).Lerp(transform.position.x, mTargetPosition.x)) ;
					transform.PositionY((1.0f - Mathf.Exp(-Time.deltaTime * 20)).Lerp(transform.position.y, mTargetPosition.y));
	
				}
			}
			
        }
    }
}
